package com.ssyc.student.adapter;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.ssyc.student.R;
import com.ssyc.student.bean.TreasureChestInfo;

import java.util.List;

/**
 * 学生端百宝箱套装列表适配器
 * Created by Administrator on 2018/4/12 0012.
 */

public class StTreasuerClothesAdapter extends BaseQuickAdapter<TreasureChestInfo.DataFashionBean, BaseViewHolder> {
    private Context context;

    public List<TreasureChestInfo.DataFashionBean> data;

    public Handler mHandler;

    public StTreasuerClothesAdapter(Context context, int layoutResId,
                                    @Nullable List<TreasureChestInfo.DataFashionBean> data,
                                   Handler mHandler) {
        super(layoutResId, data);
        this.context = context;
        this.data = data;
        this.mHandler = mHandler;
    }

    @Override
    protected void convert(BaseViewHolder helper, TreasureChestInfo.DataFashionBean item) {

        //条目
        RelativeLayout ll_item = helper.getView(R.id.ll_item);

        //图片
        ImageView lv_img = helper.getView(R.id.lv_img);

        //名称
        TextView tv_name = helper.getView(R.id.tv_name);

        tv_name.setText(item.getName());

        Glide.with(context).load(item.getUrl2()).into(lv_img);

        //选中状态
        ImageView iv_checked = helper.getView(R.id.iv_checked);

        if (item.ischeck) {
            ll_item.setBackground(context.getResources().getDrawable(
                    R.drawable.student_fashionable_itembg_sel));
            iv_checked.setVisibility(View.VISIBLE);
        } else {
            ll_item.setBackground(context.getResources().getDrawable(R.drawable.student_fashionable_itembg_normal));
            iv_checked.setVisibility(View.INVISIBLE);
        }


        //item的点击事件
        ll_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (item.ischeck) {
                    ll_item.setBackground(context.getResources().getDrawable(R.drawable.student_fashionable_itembg_normal));
                    iv_checked.setVisibility(View.INVISIBLE);

                    for(int i = 0 ; i < data.size() ; i++) {
                        data.get(i).ischeck = false;
                    }

                    Message message = new Message();
                    message.what = Integer.parseInt(item.getSub_type());
                    Bundle bundle = new Bundle();
                    bundle.putString("url","-1");
                    message.setData(bundle);
                    mHandler.sendMessage(message);

                } else {
                    ll_item.setBackground(context.getResources().getDrawable(
                            R.drawable.student_fashionable_itembg_sel));
                    iv_checked.setVisibility(View.VISIBLE);

                    for(int i = 0 ; i < data.size() ; i++) {
                        data.get(i).ischeck = false;
                    }
                    item.ischeck = true;
                    Message message = new Message();
                    message.what = Integer.parseInt(item.getSub_type());
                    Bundle bundle = new Bundle();
                    bundle.putString("url",item.getUrl());
                    message.setData(bundle);
                    mHandler.sendMessage(message);
                }

                notifyDataSetChanged();
            }
        });
    }
}
